Xbasic

STRTRAN Function

Syntax

Output_String as C = STRTRAN(C character,C substring,[C replacement,[N start_pos,[N occurrences[,N every]]]])

Arguments

characterCharacter

The character string to search.

substringCharacter

The string to find in the Input_String.

replacementCharacter

Default = "" (NULL). The string that replaces the substring.

start_posNumeric

Default = 1. Specifies which (and subsequent) occurrences will be replaced. A value N for the optional Start_Position begins the replacement at the N th occurrence of substring.

occurrencesNumeric

Default = All. Specifies the maximum number of occurrences of substring to replace with replacement.

everyNumeric

Default = 1. Specifies the interval between replacements.

Returns

Output_StringCharacter

Returns the character string produced by the replacement operation.

Description

Replaces each occurrence of a string with another.

Discussion

STRTRAN() replaces every occurrence of substring in 'character' with a replacement.

You can use STRTRAN() with memo fields.
? strtran("The Road Not Traveled", "Traveled", "Paved")
= "The Road Not Paved"

? strtran("I much prefer dogs", "dogs", "cats")
= "I much prefer cats"

You can use STRTRAN() to remove a specific substring from anywhere within another string. For example, to index a field containing titles, without the article "The", you might use the following expression:

' If EVENT stores "The Kentucky Derby",
' removes the word "The", resulting in "Kentucky Derby"
strtran(EVENT, "The ", "")

This then places "Kentucky Derby" near the other events starting with "K" instead of with the events starting with "T".

Typically, an article (i.e., "a", "an", or "the") is capitalized only when it occurs at the start of a title. If so, then capitalizing it within the substring causes the function to remove only the first occurrence. You can use a similar expression to remove "A"s and other articles at the start of character values.

The following example replaces every second letter "e" in the search string.

dim string as C
string = "this is the number one replacement function"
? stritran(string, "e", "|", 1, 99, 2) 
= "this is th| number on| replac|ment function"

Limitations

The substring argument is case sensitive.

See Also